<?php

/**
 * This is the model class for table "transporter_detail".
 *
 * The followings are the available columns in table 'transporter_detail':
 * @property integer $id_tpdetail
 * @property string $name
 * @property string $email
 * @property string $mobile
 * @property string $date_add
 * @property string $date_upd
 * @property integer $del_flg
 */
class TransporterDetail extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return TransporterDetail the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'transporter_detail';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            array('name, email', 'required'),
            array('email','unique', 'message'=>Yii::t('app', 'This email already exists.')),
            array('email', 'email'),
			array('del_flg', 'numerical', 'integerOnly'=>true),
			array('name, email', 'length', 'max'=>132),
			array('company_name', 'length', 'max'=>255),
			array('mobile, phone', 'length', 'max'=>16),
			array('info_transporter, company_name, date_add, date_upd', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_tpdetail, company_name, name, email, mobile, date_add, date_upd, del_flg, phone', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
//            'tr' => array(self::BELONGS_TO, 'Transporter', 'id_transporter')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_tpdetail' => '#',
            'id_transporter' => Yii::t('app', 'Transporter company'),
            'company_name' => Yii::t('app', 'Transporter company'),
			'name' => Yii::t('app', 'Name'),
			'email' => Yii::t('app', 'Email'),
            'info_transporter' => Yii::t('app', 'Info transporter'),
			'mobile' => Yii::t('app', 'Mobile'),
			'phone' => Yii::t('app', 'Phone'),
			'date_add' => 'Date Add',
			'date_upd' => 'Date Upd',
			'del_flg' => 'Del Flg',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_tpdetail',$this->id_tpdetail);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('phone',$this->phone,true);
        $criteria->compare('info_transporter',$this->info_transporter,true);
        $criteria->compare('company_name',$this->company_name,true);
		$criteria->compare('date_add',$this->date_add,true);
		$criteria->compare('date_upd',$this->date_upd,true);
		$criteria->compare('del_flg',$this->del_flg);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function beforeSave() {
        $now = new CDbExpression('NOW()');
        if ($this->isNewRecord) $this->date_add = $now;

        $this->date_upd = $now;

        return parent::beforeSave();
    }

    public static function _getTransporterByCompany($id ){
        $criteria=new CDbCriteria;
        $criteria->addCondition('id_tpdetail = :id_tpdetail');
        $criteria->addCondition('del_flg = 0');
        $criteria->params = array(':id_tpdetail' => (int)$id);
        $mTransporter = self::model()->findAll($criteria);
        if($mTransporter){
            return $mTransporter;
        }else{
            return false;
        }
    }

    public static function getObjTransporter(){
        $criteria=new CDbCriteria;
        $criteria->addCondition('del_flg = 0');
        $dataObj = self::model()->findAll($criteria);
        if($dataObj){
            return $dataObj;
        }else{
            return false;
        }
    }

    public static function getListTransporter($id = 0){
        if($id!=0)
            $dataObj = self::_getTransporterByCompany($id);
        else
            $dataObj = self::getObjTransporter();

        $result = array();
        if($dataObj){
            foreach($dataObj as $data){
                $result[$data->id_tpdetail] = $data->name;
            }
        }
        return $result;
    }
}